package lib.drumpicker;

import java.util.Calendar;

import jp.jig.test.alarm.Util;
import jp.jig.test.alarm.ViewUtil;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;

/**
 * 
 * @author yagi
 * 
 */
public class DateDrumPicker extends DrumPicker {
	private final static String TAG = DateDrumPicker.class.getSimpleName();
	private final static String[] YEAR = {
		"2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026", "2027", "2028", "2029", "2030"
	};
	private final static String[] MONTH = {
		"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
	};
	private final static String[] DAYS = {
		"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"
	};
	DatePicker.OnDateChangedListener mListener = null;
	private int mYear = 0;
	private int mMonth = 0;
	private int mDay = 0;
	private Calendar mCalendar = null;

	public DateDrumPicker(Context context) {
		this(context, null);
	}

	public DateDrumPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		mCalendar = Calendar.getInstance();
		addRow(YEAR, 68);
		addRow(MONTH, 68);
		addRow(DAYS, 68);
		int yearPos = 0;
		String year = Integer.toString(mCalendar.get(Calendar.YEAR));
		for (int i = 0; i < YEAR.length; i++) {
			String y = YEAR[i];
			if (year.equals(y)) {
				yearPos = i;
				break;
			}
		}
		setPosition(0, yearPos);
		setPosition(1, mCalendar.get(Calendar.MONTH));
		setPosition(2, mCalendar.get(Calendar.DATE) - 1);
		// リスナー
		setOnPostionChangedListener(new OnPositionChangedListener() {
			@Override
			public void onPositionChanged(int itemPos, int pos) {
				if (pos < 0) {
					return;
				}
				switch (itemPos) {
					case 0:
						mYear = Integer.parseInt(YEAR[pos]);
						resizeDay(mYear, mMonth);
						break;
					case 1:
						mMonth = Integer.parseInt(MONTH[pos]);
						resizeDay(mYear, mMonth);
						break;
					case 2:
						int adjust = monthDays(mYear, mMonth);
						if (pos > adjust - 1) {
							pos = adjust - 1;
						}
						mDay = Integer.parseInt(DAYS[pos]);
				}
				if (mListener != null) {
					mListener.onDateChanged(null, mYear, mMonth, mDay);
				}
			}
		});
	}

	private void resizeDay(int year, int month) {
		// 日
		int oldy = mCalendar.get(Calendar.YEAR);
		int oldm = mCalendar.get(Calendar.MONTH);
		int oldd = mCalendar.get(Calendar.DAY_OF_MONTH);

		mCalendar.set(Calendar.YEAR, year);
		mCalendar.set(Calendar.MONTH, month);
		mCalendar.set(Calendar.DAY_OF_MONTH, 1);
		mCalendar.add(Calendar.DAY_OF_MONTH, -1);
		final int day = mCalendar.get(Calendar.DAY_OF_MONTH);
		////Log.d(TAG, "day=" + day + "::" + month);

		resize(2, new IsGoneListener() {
			@Override
			public boolean isGone(View item, int pos) {
				if (pos > day) {
					return true;
				}
				return false;
			}
		});

		mCalendar.set(Calendar.YEAR, oldy);
		mCalendar.set(Calendar.MONTH, oldm);
		mCalendar.set(Calendar.DAY_OF_MONTH, oldd);
	}

	public void setOnDateChangedListener(DatePicker.OnDateChangedListener listener) {
		mListener = listener;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int height = ViewUtil.getDip2Pixel(getContext(), DrumPicker.LINE_HEIGHT * 3);
		int h = MeasureSpec.makeMeasureSpec(MeasureSpec.getMode(heightMeasureSpec), height);
		super.onMeasure(widthMeasureSpec, h);
	}

	boolean isGone(View item) {
		return false;
	}

	public void setYear(int year) {
		if (year >= 2012 && year < 2030) {
			setPosition(0, year - 2012);
			mYear = year;
		}
	}

	public void setMonth(int month) {
		if (month >= 1 && month < 13) {
			setPosition(1, month - 1);
			mMonth = month;
		}
	}

	public void setDay(int day) {
		if (day >= 1 && day < 32) {
			int days = monthDays(mYear, mMonth);
			if (day > days) {
				day = days;
			}
			setPosition(2, day - 1);
			mDay = day;
		}
	}

	public int getDate() {
		//int y = Integer.parseInt(YEAR[getCount(0)]);
		//int m = Integer.parseInt(MONTH[getCount(1)]);
		//int d = Integer.parseInt(DAYS[getCount(2)]);
		return Util.dateCombine(mYear, mMonth, mDay);
	}

	static int monthDays(int year, int month) {
		switch (month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				return 31;
			case 4:
			case 6:
			case 9:
			case 11:
				return 30;
			case 2:
				if (isLeapYear(year)) {
					return 29;
				} else {
					return 28;
				}
		}
		return -1;
	}

	static boolean isLeapYear(int year) {
		if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)) {
			return true;
		} else {
			return false;
		}
	}

}
